﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Krypton.Toolkit;
using TSNPro.Device;

namespace TSNPro.MyControl
{
    public partial class TSNCommonControl : UserControl
    {
        private CommonDevice _device;
        public TSNCommonControl()
        {

            InitializeComponent();

            //KryptonTextBox txtSetValue = new KryptonTextBox();
            //txtSetValue.Dock = DockStyle.Fill;
            //txtSetValue.Text = "Hello";
            //txtSetValue.Size = new System.Drawing.Size(200, 20);
            ////txtSetValue.DataBindings.Add("Text", item, "DisplayValue");
            //tabLaySet.Controls.Add(txtSetValue, 2, 1);
        }

        public void SetDevice(CommonDevice device)
        {
            _device = device;
            List<CommonDataItem>  items = _device.GetCommonDataItems();
            InitMonitorControl(items);
        }

        private void InitMonitorControl(List<CommonDataItem> items)
        {
            for (int i = 0; i < items.Count; i++) 
            {
                CommonDataItem item = items[i];
                TextBox txtSetValue = tabLayBurning.GetControlFromPosition(1,i+1) as TextBox;
                txtSetValue.Text = item.SetValue.ToString();
                //txtSetValue.DataBindings.Add(new Binding("Text", item, "SetValue",false,DataSourceUpdateMode.Never));

                TextBox txtDisplayValue = tabLayBurning.GetControlFromPosition(2, i + 1) as TextBox;
                
                txtDisplayValue.DataBindings.Add(new Binding("Text", item, "DisplayValue", false, DataSourceUpdateMode.OnValidation));
                txtDisplayValue.DataBindings.Add(new Binding("ForeColor", item, "FontColor", false, DataSourceUpdateMode.OnValidation));

                TextBox txtErrorValue = tabLayBurning.GetControlFromPosition(3, i + 1) as TextBox;
                txtErrorValue.Text = item.ErrorValue.ToString();
                //txtErrorValue.DataBindings.Add(new Binding("Text", item, "ErrorValue", false, DataSourceUpdateMode.Never));

                KryptonButton btn  = tabLayBurning.GetControlFromPosition(4, i + 1) as KryptonButton;
                btn.Click += (sender, e) =>
                {
                    item.SetValue = int.Parse(txtSetValue.Text);
                    item.ErrorValue = int.Parse(txtErrorValue.Text);
                    item.OnPropertyChanged(nameof(item.FontColor));
                };
            }
            
        }



        private void btnCarrierGas_Click(object sender, EventArgs e)
        {

        }
    }
}
